package com.yunhuajinfu.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yunhuajinfu.R;

/**
 * Created by ekikousei易皇星 on 16/11/15.
 * E-mail:13764664731@163.com
 * Signature:缘分是本书,翻的不经意会错过,读的太认真会流泪!!
 * <p/>
 * TODO:类描述:
 */
public class TitleBar extends LinearLayout implements  View.OnClickListener {
    private View view;
    private RelativeLayout titlebar_rl_bag_color;
    private TextView titlebar_tv_center_text,titlebar_right_text;
    private ImageView titlebar_iv_back,titlebar_iv_rignt_img;

    //
    private  ClickCallback mClickCallback;

    public TitleBar(Context context) {
        super(context);
        init(context);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        view= LayoutInflater.from(context).inflate(R.layout.titlebar_layout, this, true);
        init(context);
    }

    private void init(Context context) {
        titlebar_rl_bag_color= (RelativeLayout) view.findViewById(R.id.titlebar_rl_bag_color);
        titlebar_tv_center_text= (TextView) view.findViewById(R.id.titlebar_tv_center_text);
        titlebar_iv_back= (ImageView) view.findViewById(R.id.titlebar_iv_back);
        titlebar_iv_rignt_img= (ImageView) view.findViewById(R.id.titlebar_iv_rignt_img);
        titlebar_right_text= (TextView) view.findViewById(R.id.titlebar_right_text);
    }

    /**
     *
      * @param msg 设置文本,和事件监听
     */
    public void setTitleBarCenterText(String msg,View.OnClickListener mlistener){
        titlebar_tv_center_text.setText(msg);
        titlebar_iv_back.setOnClickListener(mlistener);
    }

    /**
     *
     * @param color  设置titlebar 背景颜色
     */
    public void setTitleBarBackgroundColor(int color){
        titlebar_rl_bag_color.setBackgroundColor(color);
    }

    /**
     *
     * @param imageBack
     * @param centerMesg
     * @param imageRight
     */
    public  void setTitleBar(int imageBack,String centerMesg,int imageRight){
        titlebar_iv_back.setImageResource(imageBack);
        titlebar_tv_center_text.setText(centerMesg);
        titlebar_iv_rignt_img.setImageResource(imageRight);


    }

    public void setTitleBarTextColor(int color){
        titlebar_tv_center_text.setTextColor(color);
    }

    /**
     *
     * @param mlicklistener  事件监听
     */
    public void setTitleBarLeftListener(View.OnClickListener mlicklistener){
        titlebar_iv_back.setOnClickListener(mlicklistener);
        titlebar_iv_rignt_img.setOnClickListener(mlicklistener);
        titlebar_right_text.setOnClickListener(mlicklistener);

    }

    public void setTitleBarRightText(String msg){
        titlebar_right_text.setText(msg);
    }



    public void setTitleBackgroundColor(int color){
        titlebar_rl_bag_color.setBackgroundColor(color);
    }
    public void setTitlebarRightTextColor(int color){
        titlebar_right_text.setTextColor(color);

    }


    /**
     *   导航栏点击事件 接口回调
     */
    interface  ClickCallback{

        void onBackClick();
        void onRightClick();
    }

    /**
     *
     * @param mClickCallback  回调监听
     */
    public  void setClickCallback(ClickCallback mClickCallback){
        this.mClickCallback=mClickCallback;
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case  R.id.titlebar_iv_back:
                mClickCallback.onBackClick();

            break;

            case  R.id.titlebar_iv_rignt_img:

                mClickCallback.onRightClick();

                break;

            default:
        }
    }


}
